# Maintainer: Antoine Martin <antoine@devloop.org.uk>

_realname=gssapi
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.9.0
pkgrel=2
pkgdesc="A python interface to RFC 2743/2744 (plus common extensions) (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/pythongssapi/python-gssapi'
msys2_references=(
  'purl: pkg:pypi/gssapi'
)
license=('spdx:ISC')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-decorator"
         "${MINGW_PACKAGE_PREFIX}-gss")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=(!strip)
source=("https://pypi.org/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        001-use-distutils-from-stdlib.patch)
sha256sums=('f468fac8f3f5fca8f4d1ca19e3cd4d2e10bd91074e7285464b22715d13548afe'
            'f042e9e98a8be163e15b815d058d82e787afc1f382296693f560a4b0fce4b882')
export MSYS=winsymlinks:lnk

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/001-use-distutils-from-stdlib.patch
  sed -i -e 's+elif winkrb_path:+elif False:+g' setup.py
  sed -i -e 's+gssapi/gssapi.h+gss.h+g' gssapi/raw/python_gssapi.h
  sed -i -e 's+gssapi/gssapi.h+gss.h+g' gssapi/raw/python_gssapi_ext.h
  sed -i -e 's+gssapi/gssapi_krb5.h+gss.h+g' gssapi/raw/python_gssapi_krb5.h
  cat > gssapi/raw/mech_krb5.pyx <<EOF
from libc.stdint cimport uint32_t
cdef extern from "python_gssapi.h":
    ctypedef uint32_t OM_uint32
    ctypedef struct gss_OID_desc:
        OM_uint32 length
        void *elements
    ctypedef gss_OID_desc* gss_OID
def make_OID(s):
    import struct
    v = struct.pack("@Is", len(s), s)
    return v
pkrb5 = make_OID(b"\x2A\x86\x48\x86\xF7\x12\x01\x02\x02")
cdef char *gss_mech_krb5_str = pkrb5
pprincipal = make_OID(b"\x2A\x86\x48\x86\xF7\x12\x01\x02\x02");
cdef char *GSS_KRB5_NT_PRINCIPAL_NAME_str = pprincipal
cdef gss_OID gss_mech_krb5 = <gss_OID> gss_mech_krb5_str
cdef gss_OID GSS_KRB5_NT_PRINCIPAL_NAME = <gss_OID> GSS_KRB5_NT_PRINCIPAL_NAME_str
EOF

  cd "$srcdir"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-$pkgver" "python-build-${MSYSTEM}"
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  GSSAPI_MAIN_LIB=${MINGW_PREFIX}/bin/libgss-3.dll \
  GSSAPI_LINKER_ARGS="-lgss" \
  GSSAPI_COMPILER_ARGS="-fPIC" \
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    "${MINGW_PREFIX}"/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
